var loanId = $("#loan").data("id");

$(function () {

    var loadSubscribe = function () {
        // 初始化列表
        var table = $('#subscribeTable').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "loan/getSubscribeData",
                "data": function (d) {
                    d.id = loanId;
                },
                "type": "POST"
            },
            "aoColumnDefs": [
                {
                    "targets": 0, // 用户名
                    "mRender": function (data, type, row) {
                        return '<a href="user/index/' + row.userId + '">' + row.loginName + '</a>';
                    }
                }, {
                    "targets": 1, // 标的名称
                    "mRender": function (data, type, row) {
                        return '<a href="loan/detail/' + row.loanId + '">' + row.title + '</a>';
                    }
                }, {
                    "targets": 2, // 操作时间
                    "mRender": function (data, type, row) {
                        return row.recordTime !== null
                        ? $.format.date(new Date(row.recordTime), "yyyy/MM/dd HH:mm")
                        : '-';
                    }
                }, {
                    "targets": 3, // 金额
                    "mRender": function (data, type, row) {
                        return row.amount;
                    }
                }
            ],
            "order": [2, 'desc'],
            "oLanguage": chinese,
            "language": {
                "decimal": ",",
                "thousands": ",",
            },
            "iDisplayLength": 25,
            "aLengthMenu": [
                [5, 10, 25, 50, 100, -1],
                [5, 10, 25, 50, 100, "全部"]
            ],
            "sDom": '<"dt-panelmenu clearfix"l<"pull-right"Tf>r>t<"dt-panelfooter clearfix"ip>',
            "oTableTools": {
                "aButtons": [{
                        "sExtends": "xls",
                        "sButtonText": "导出为Excel"
                    }
                ],
                "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
            },
            "initComplete": function (settings, json) {
            }
        });
    };

    var initPage = function () {
        // 更新tab状态
        $("#tab-subscribe").removeClass("btn-default").addClass("btn-primary");
        loadSubscribe();
    };

    // 初始化页面
    initPage();

    /**
     * 添加定向标的用户显示
     * @returns 
     */
    $("#add_subscribe_name").on('click', function () {
        $('#myModal').modal('show');
    });
    /**
     * 添加定向标的用户
     * @returns 
     */
    $("#confirm_deduction").on('click', function () {
        var loanid = $("#loan").data("id");
        var loginName = $("#subscribe_loginname").val();
        
        if (loginName === '') {
            alert("请填写用户登录名");
            return;
        }
        
        $.ajax({
            url: "loan/saveSubscribe",
            data: {loanId: loanid, loginName: loginName},
            type: 'POST',
            async: false,
            success: function (res) {
                if (res.success) {
                    $('#myModal').modal('hide');
                    location.reload();
                } else {
                    alert(res.message);
                }
            }
        }).fail(function () {
            alert('服务器错误');
        });
    });
});
